import ORM, { DatabaseKind } from '../../../utilities/ORM' import * as Service from '../../../services/entities/Note' import Model from '../../../models/Note' export default async (req, res) => { const orm = new ORM({ kind: process.env.DATABASE_DIALECT as DatabaseKind, url: process.env.DATABASE_URL, }) const repository = orm.getRepository(Model) const methodHandlers = { 'GET': Service.getSingle(repository), 'PUT': Service.save(repository)(req.body), 'DELETE': Service.remove(repository) } const { [req.method as keyof typeof methodHandlers]: handler = null } = methodHandlers if (handler === null) { res.statusCode = 415 res.json({ message: 'Method not allowed.' }) return } const { id } = req.query try { const { status, ...etcReturn } = await handler(id) res.statusCode = status if (etcReturn['data']) { res.json(etcReturn['data']) return } res.end() } catch (err) { console.error(err) const { status, data, } = err res.statusCode = status || 500 if (data && status !== 204) { res.json(data) return } res.end() } }